package InventoryAssetCheckoutDetail

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_service/client/inventory_service"

	"property_api/internal/svc"
	"property_api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PagesLogic {
	return &PagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Pages 查询资产出库明细的分页逻辑
func (l *PagesLogic) Pages(req *types.InventoryAssetCheckoutDetailPageReq) (resp *types.PageRes, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)

	// 构建查询条件对象
	where := &inventory_service.InventoryAssetCheckoutDetail{
		TenantID:        tenantID,            // 租户ID
		ID:              req.ID,              // 记录ID
		CheckoutID:      req.CheckoutID,      // 出库单ID
		AssetID:         req.AssetID,         // 资产ID
		AssetCode:       req.AssetCode,       // 资产编号
		AssetName:       req.AssetName,       // 资产名称
		Category:        req.Category,        // 分类
		CategoryID:      req.CategoryID,      // 分类ID
		SpecModel:       req.SpecModel,       // 规格型号
		SpecModelID:     req.SpecModelID,     // 规格型号ID
		Brand:           req.Brand,           // 品牌
		BrandID:         req.BrandID,         // 品牌ID
		Origin:          req.Origin,          // 产地
		OriginID:        req.OriginID,        // 产地ID
		Unit:            req.Unit,            // 单位
		UnitPrice:       req.UnitPrice,       // 单价
		Quantity:        req.Quantity,        // 数量
		StorageLocation: req.StorageLocation, // 存放地点
		Manufacturer:    req.Manufacturer,    // 生产商
		ManufacturerID:  req.ManufacturerID,  // 生产商ID
	}

	// 调用服务层方法，查询资产出库明细记录的分页数据
	// 服务层方法 InventoryAssetCheckoutDetailPage 会处理实际的数据库查询操作
	data, err := l.svcCtx.InventoryService.InventoryAssetCheckoutDetailPage(l.ctx, &inventory_service.InventoryAssetCheckoutDetailPageReq{
		Page:  req.Page,  // 当前页码
		Limit: req.Limit, // 每页条数
		Where: where,     // 查询条件
	})

	// 返回分页数据的响应
	return result.SucPage(data, types.InventoryAssetCheckoutDetail{}).PageEd()
}
